//
//  PopoverAnimator.swift
//  SwiftWeibo
//
//  Created by Lucky on 16/5/9.
//  Copyright © 2016年 Lucky. All rights reserved.
//

import UIKit

let PopoverAnimatorWillShow = "PopoverAnimatorWillShow"
let PopoverAnimatorWillDismiss = "PopoverAnimatorWillDismiss"

class PopoverAnimator: NSObject {
    var presentFrame = CGRectZero
    var isPresent: Bool = false
}

extension PopoverAnimator: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning
{
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        let pc = PopoverPresentionController(presentedViewController: presented, presentingViewController: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        NSNotificationCenter.defaultCenter().postNotificationName(PopoverAnimatorWillShow, object: self)
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        NSNotificationCenter.defaultCenter().postNotificationName(PopoverAnimatorWillDismiss, object: self)
        return self
    }
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        if isPresent {
            //1，拿到展现视图
            
            // system - UITransitionContextToViewControllerKey, and UITransitionContextFromViewControllerKey.
            // toVC是展现的视图
            //        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
            //        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
            //        print(toVC)
            //        print(fromVC)
            
            // UITransitionContextFromViewKey, and UITransitionContextToViewKey
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            //        print(toView)
            
            transitionContext.containerView()?.addSubview(toView)
            
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            
            toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                toView.transform = CGAffineTransformIdentity
            }) { (_) in
                //2，动画执行完毕一定要告诉系统
                transitionContext.completeTransition(true)
            }
        } else {
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })
        }
        
    }
}